package com.corgi;

import java.util.List;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.util.Log;

/**
 * @author zhouhui 设置界面
 */
public class SettingsActivity extends PreferenceActivity implements
		SharedPreferences.OnSharedPreferenceChangeListener {
	private Messenger serviceMessenger = null;
	private boolean isBound;

	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
			serviceMessenger = new Messenger(service);
		}

		public void onServiceDisconnected(ComponentName className) {
			serviceMessenger = null;
		}
	};

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.settings);
		PreferenceManager.getDefaultSharedPreferences(this)
				.registerOnSharedPreferenceChangeListener(this);

		PreferenceScreen preferenceScreen = getPreferenceScreen();
		PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("setapp");

		PackageManager packageManager = getPackageManager();
		List<PackageInfo> packageInfoList = packageManager
				.getInstalledPackages(0);
		for (PackageInfo info : packageInfoList) {
			CheckBoxPreference cbPref = new CheckBoxPreference(this);
			cbPref.setKey(info.packageName);
			cbPref.setTitle(info.applicationInfo.loadLabel(packageManager)
					.toString());
			cbPref.setIcon(info.applicationInfo.loadIcon(packageManager));
			preferenceCategory.addPreference(cbPref);
		}

		Intent j = new Intent(this, LockService.class);
		j.putExtra("clearhistory", true);
		startService(j);

		Intent serviceIntent = new Intent(this, LockService.class);
		bindService(serviceIntent, mConnection, 0);
		isBound = true;
	}
	
	@Override
	@Deprecated
	public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
			Preference preference) {
		
		return super.onPreferenceTreeClick(preferenceScreen, preference);
	}

	protected void onDestroy() {
		super.onDestroy();
		Log.e("blueberry", "监isBound="+isBound);
		/*if (isBound) {
			unbindService(mConnection);
			isBound = false;
		}*/
	}

	// 监听当前哪个listpreference 被点中
	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		Log.e("blueberry", "监听变化 key="+key);
		Log.e("blueberry", "isBound="+isBound);
		if (isBound) {
			if (serviceMessenger != null) {
				try {
					//点击不是设置密码项
					if (!key.equals("setpassword")) {
						SharedPreferences sharedPrefs = PreferenceManager
								.getDefaultSharedPreferences(this);
						Boolean check = sharedPrefs.getBoolean(key, false);
						Log.e("blueberry", "check="+check);
						if (check == false) {
							sharedPrefs.edit().remove(key + "login").apply();
							sharedPrefs.edit().commit();
						} else {
							sharedPrefs.edit().putBoolean(key + "login", false)
									.apply();
							sharedPrefs.edit().commit();
						}
					}

					Message msg = Message.obtain(null,
							LockService.PREF_CHANGED, key);
					serviceMessenger.send(msg);
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
